package com.example.fence.service.impl;

import com.example.fence.Task;
import com.example.fence.entity.Alarm;
import com.example.fence.entity.DeleteDevice;
import com.example.fence.entity.Fence;
import com.example.fence.enums.AlarmMode;
import com.example.fence.util.FenceStoreUtil;
import com.example.fence.util.Result;
import com.example.fence.vo.AlarmVo;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.example.fence.util.FenceStoreUtil.fenceDeviceList;
import static com.example.fence.util.FenceStoreUtil.imeiToPointList;
import static com.example.fence.util.ResultCode.SUCCESS;
//import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
@Slf4j
@Service
public class AlarmServiceImpl {
    public Result createAlarmType(Alarm alarm) throws MqttException {

        for(String imei:alarm.getDevicesName()){
            // 设置报警类型-----直接覆盖原来的
            FenceStoreUtil.deviceAlarmMode.put(imei,alarm.getAlarmMode());
            log.info("Create AlarmMode: imei-{},mode:{}",imei,FenceStoreUtil.deviceAlarmMode.get(imei).toString());
            // 在原先数据上添加的代码(写错了嗷，留着以后可能用)
//            if(FenceStoreUtil.deviceAlarmMode.get(imei)==null){  // 原来不存在则放入
//                FenceStoreUtil.deviceAlarmMode.put(imei,alarm.getAlarmMode());
//                log.info("Create AlarmMode: imei-{},mode:{}",imei,FenceStoreUtil.deviceAlarmMode.get(imei).toString());
//            } else {  // 原来存在则添加
//                log.info("Update AlarmMode: before imei-{},mode:{}",imei,FenceStoreUtil.deviceAlarmMode.get(imei).toString());
//                FenceStoreUtil.deviceAlarmMode.get(imei).addAll(alarm.getAlarmMode());
//                log.info("Update AlarmMode: after imei-{},mode:{}",imei,FenceStoreUtil.deviceAlarmMode.get(imei).toString());
//            }

            // 设置发送邮箱
            if(alarm.getSendEmail()!=null && !alarm.getSendEmail().isEmpty()){
                log.info("SendEmail created, before:{}",FenceStoreUtil.deviceSendEmail.get(imei));
                FenceStoreUtil.deviceSendEmail.put(imei,alarm.getSendEmail());
                log.info("SendEmail created, after:{}",FenceStoreUtil.deviceSendEmail.get(imei));
            }
            // 设置接收邮箱-----直接覆盖原来的
            FenceStoreUtil.deviceReceiveEmail.put(imei,alarm.getReceiveEmail());

            //设置发送间隔-----直接覆盖原来的
            FenceStoreUtil.sendInterval.put(imei,alarm.getSendInterval());
            // 在原先数据上添加的代码(写错了嗷，留着以后可能用)
//            if(alarm.getReceiveEmail()!=null && !alarm.getReceiveEmail().isEmpty()){
//                if(FenceStoreUtil.deviceReceiveEmail.get(imei)==null){ // 原来不存在则插入
//                    FenceStoreUtil.deviceReceiveEmail.put(imei,alarm.getReceiveEmail());
//                } else { // 原来存在则更新
//                    log.info("ReceiveEmail updated, before:{}",FenceStoreUtil.deviceReceiveEmail.get(imei).toString());
//                    FenceStoreUtil.deviceReceiveEmail.get(imei).addAll(alarm.getReceiveEmail());
//                    log.info("ReceiveEmail updated, after:{}",FenceStoreUtil.deviceReceiveEmail.get(imei).toString());
//                }
//
//            }
            //Alarm上传TB
            Task.PostAlarm(imei,alarm.getAlarmMode(),alarm.getReceiveEmail(),alarm.getSendEmail(),alarm.getSendInterval());

        }

        // 返回设置结果
        return new Result(SUCCESS.code,true,alarm,"success");
    }
    public Result deleteDevice(DeleteDevice deleteDevice) throws MqttException {

        for(String imei:deleteDevice.getDevicesName()){
           // 0.若上锁则解锁
            System.out.println(FenceStoreUtil.imeiLock.get(imei));
            if (FenceStoreUtil.imeiLock.containsKey(imei)&&FenceStoreUtil.imeiLock.get(imei)) {  // 将报警设置为false
                FenceStoreUtil.imeiLock.put(imei, false);
                Task.PostUnC2(imei);
            }
            // 1. 移除在线表中该设备信息
            // 1.1 fenceDeviceList(pointList-deviceList表)，先移除这个才能删imeiToPointList (imei-PointList表)
            if(imeiToPointList.get(imei)!=null){
                List<String> path=imeiToPointList.get(imei);
                // 移除pointList-deviceList表中该imei信息
                if(fenceDeviceList.get(path)!=null){  // 不需要判断里面有没有imei
                    fenceDeviceList.get(path).remove(imei);
                }
            }
            // 1.2 移除imei-PointList表中该imei信息
            imeiToPointList.remove(imei);
            // 1.3 移除其他表中的信息，除了 Map<String, Boolean> deleteFence
            FenceStoreUtil.imeiLock.remove(imei);
            FenceStoreUtil.outFenceCounts.remove(imei);
            FenceStoreUtil.deviceAlarmMode.remove(imei);
            FenceStoreUtil.deviceSendEmail.remove(imei);
            FenceStoreUtil.deviceReceiveEmail.remove(imei);
            FenceStoreUtil.sendInterval.remove(imei);
            FenceStoreUtil.lastSendTime.remove(imei);

            //Alarm上传TB
            Task.PostDelete(imei);

            log.info("已经删除{}设备围栏",imei);
        }


        // 返回设置结果
        return new Result(SUCCESS.code,true,deleteDevice,"success");
    }

    public Result getAlarm(String imei){
        Map<String,Object> resultMap=new HashMap<>();

        // imei号
        resultMap.put("imei",imei);
        // 电子围栏路径
        resultMap.put("path",FenceStoreUtil.imeiToPointList.get(imei));
        // 报警方式
        resultMap.put("alarmMode",null); // 先设为null
        Set<Integer> deviceAlarmModeCode=FenceStoreUtil.deviceAlarmMode.get(imei);
        if(deviceAlarmModeCode!=null && !deviceAlarmModeCode.isEmpty()){
            // 枚举数字转为原名称
            List<String> deviceAlarmModeName=new LinkedList<>();
            for(Integer modeCode:deviceAlarmModeCode){
                deviceAlarmModeName.add(AlarmMode.getNameByCode(modeCode));
            }
            resultMap.put("alarmMode",deviceAlarmModeName);
        }
        // 发送邮箱
        resultMap.put("sendEmail",FenceStoreUtil.deviceSendEmail.get(imei));
        // 接收邮箱
        resultMap.put("receiveEmail",FenceStoreUtil.deviceReceiveEmail.get(imei));


        return new Result(SUCCESS.code,true,resultMap,"success");
    }
}
